package top.ivansong92.example.leetcode.learning.data.struct.array.extension;

import java.util.Arrays;

public class NewArray {
    private int[] data;
    private int size;

    public NewArray() {
        this(16);
    }

    public NewArray(int capacity) {
        data = new int[capacity];
        size = 0;
    }

    public int size() {
        return size;
    }

    public int capatity() {
        return data.length;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    //增
    public void addFirst(int value) {
        add(value, 0);
    }

    public void addLast(int value) {
        add(value, size);
    }

    public void add(int value) {
        addLast(value);
    }

    public void add(int value, int index) {
        if (index < 0 || index > data.length) {
            throw new RuntimeException("index is ill");
        }

        if (size == Integer.MAX_VALUE) {
            throw new RuntimeException("array is full");
        }

        if (size >= data.length) {
            resize(data.length << 1);
        }

        for (int i = size; i > index; i--) {
            data[i] = data[i - 1];
        }

        data[index] = value;
        size++;
    }

    //删
    public void delete() {
        remove(1);
    }

    public int removeFirst() {
        return remove(0);
    }

    public int removeLast() {
        return remove(size - 1);
    }

    /**
     * 删除
     *
     * @param index
     * @return
     */
    public int remove(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("delete failed, index is " + index);
        }
        int val = data[index];
        for (int i = index; i < size; i++) {
            data[i] = data[i + 1];
        }
        size--;
        if (size == data.length / 4) {
            resize(size);
        }
        return val;
    }

    public void removeElement(int e) {
        int index = getIndex(e);
        if (index >= 0) {
            remove(index);
        }
    }

    //改
    //查
    public int get(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("get failed, index is " + index);
        }
        return data[index];
    }

    public boolean contains(int e) {
        for (int k : data) {
            if (k == e) {
                return true;
            }

        }
        return false;
    }

    public int getIndex(int e) {
        for (int i = 0; i < size; i++) {
            if (data[i] == e) {
                return i;
            }
        }
        return -1;
    }

    private void resize(int newLen) {
        if (newLen < 1) {
            newLen = 1;
        }
        data = Arrays.copyOf(data, newLen);
    }

    @Override
    public String toString() {
        return "NewArray{" +
                "data=" + Arrays.toString(Arrays.copyOfRange(data, 0, size)) +
                ", size=" + size +
                '}';
    }


    public static void main(String[] args) {
        int i = Integer.MAX_VALUE;
        System.out.println(i << 1);
        NewArray newArray = new NewArray();
        newArray.addFirst(123);
        newArray.addFirst(234);
        System.out.println(newArray);
    }
}
